home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / update1a / frmftp.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-07-26  |  17.9 KB  |  553 lines

  1. VERSION 5.00
  2. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0"; "COMCTL32.OCX"
  3. Begin VB.Form frmVBFTPJR 
  4.    Caption         =   "FTP for WebNet Browser"
  5.    ClientHeight    =   6816
  6.    ClientLeft      =   60
  7.    ClientTop       =   348
  8.    ClientWidth     =   9228
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   6816
  11.    ScaleWidth      =   9228
  12.    StartUpPosition =   2  'CenterScreen
  13.    Begin VB.OptionButton optAscii 
  14.       Caption         =   "Ascii"
  15.       Height          =   195
  16.       Left            =   3480
  17.       TabIndex        =   20
  18.       Top             =   2640
  19.       Width           =   732
  20.    End
  21.    Begin VB.OptionButton optBin 
  22.       Caption         =   "Binary"
  23.       Height          =   375
  24.       Left            =   3480
  25.       TabIndex        =   19
  26.       Top             =   2280
  27.       Width           =   732
  28.    End
  29.    Begin VB.CommandButton cmdPut 
  30.       Caption         =   "Download"
  31.       Height          =   300
  32.       Left            =   3480
  33.       TabIndex        =   18
  34.       Top             =   3360
  35.       Width           =   1092
  36.    End
  37.    Begin VB.CommandButton cmdGet 
  38.       Caption         =   "Upload"
  39.       Height          =   300
  40.       Left            =   3480
  41.       TabIndex        =   17
  42.       Top             =   3000
  43.       Width           =   1092
  44.    End
  45.    Begin ComctlLib.TreeView TreeView1 
  46.       Height          =   4452
  47.       Left            =   120
  48.       TabIndex        =   16
  49.       Top             =   2280
  50.       Width           =   3252
  51.       _ExtentX        =   5736
  52.       _ExtentY        =   7853
  53.       _Version        =   327680
  54.       Style           =   7
  55.       Appearance      =   1
  56.    End
  57.    Begin VB.FileListBox File1 
  58.       Height          =   5256
  59.       Left            =   6840
  60.       TabIndex        =   15
  61.       Top             =   1440
  62.       Width           =   2052
  63.    End
  64.    Begin VB.DirListBox Dir1 
  65.       Height          =   5256
  66.       Left            =   4680
  67.       TabIndex        =   14
  68.       Top             =   1440
  69.       Width           =   2052
  70.    End
  71.    Begin VB.DriveListBox Drive1 
  72.       Height          =   288
  73.       Left            =   4680
  74.       TabIndex        =   13
  75.       Top             =   1080
  76.       Width           =   4212
  77.    End
  78.    Begin VB.CheckBox chkPassive 
  79.       Caption         =   "Passive FTP syntax"
  80.       Height          =   255
  81.       Left            =   6600
  82.       TabIndex        =   12
  83.       Top             =   360
  84.       Width           =   1692
  85.    End
  86.    Begin VB.CommandButton cmdDisconnect 
  87.       Caption         =   "Disconnect"
  88.       Height          =   250
  89.       Left            =   2400
  90.       TabIndex        =   11
  91.       Top             =   480
  92.       Width           =   1092
  93.    End
  94.    Begin VB.CommandButton cmdConnect 
  95.       Caption         =   "Connect"
  96.       Height          =   250
  97.       Left            =   2400
  98.       TabIndex        =   10
  99.       Top             =   120
  100.       Width           =   1092
  101.    End
  102.    Begin VB.TextBox txtPassword 
  103.       Height          =   288
  104.       IMEMode         =   3  'DISABLE
  105.       Left            =   1680
  106.       PasswordChar    =   "*"
  107.       TabIndex        =   9
  108.       ToolTipText     =   "Your Password"
  109.       Top             =   1440
  110.       Width           =   2892
  111.    End
  112.    Begin VB.TextBox txtUser 
  113.       Height          =   288
  114.       Left            =   1680
  115.       TabIndex        =   7
  116.       ToolTipText     =   "Your User Name"
  117.       Top             =   1080
  118.       Width           =   2892
  119.    End
  120.    Begin VB.TextBox txtServer 
  121.       Height          =   288
  122.       Left            =   3600
  123.       TabIndex        =   4
  124.       ToolTipText     =   "Server Name"
  125.       Top             =   360
  126.       Width           =   2772
  127.    End
  128.    Begin VB.CommandButton cmdClosehOpen 
  129.       Caption         =   "Close Internet Session"
  130.       Height          =   250
  131.       Left            =   240
  132.       TabIndex        =   3
  133.       Top             =   480
  134.       Width           =   1932
  135.    End
  136.    Begin VB.TextBox txtProxy 
  137.       Height          =   288
  138.       Left            =   2640
  139.       TabIndex        =   1
  140.       Top             =   1800
  141.       Width           =   1932
  142.    End
  143.    Begin VB.CommandButton cmdInternetOpen 
  144.       Caption         =   "Start Internet Session"
  145.       Height          =   250
  146.       Left            =   240
  147.       TabIndex        =   0
  148.       Top             =   120
  149.       Width           =   1935
  150.    End
  151.    Begin ComctlLib.ImageList ImageList1 
  152.       Left            =   2280
  153.       Top             =   6240
  154.       _ExtentX        =   995
  155.       _ExtentY        =   995
  156.       BackColor       =   -2147483643
  157.       MaskColor       =   12632256
  158.       _Version        =   327680
  159.    End
  160.    Begin VB.Label label4 
  161.       Caption         =   "Password:"
  162.       Height          =   252
  163.       Left            =   120
  164.       TabIndex        =   8
  165.       Top             =   1440
  166.       Width           =   852
  167.    End
  168.    Begin VB.Label Label3 
  169.       Caption         =   "User Name:"
  170.       Height          =   252
  171.       Left            =   120
  172.       TabIndex        =   6
  173.       Top             =   1080
  174.       Width           =   972
  175.    End
  176.    Begin VB.Label Label2 
  177.       Caption         =   "FTP Server Name:"
  178.       Height          =   252
  179.       Left            =   3600
  180.       TabIndex        =   5
  181.       Top             =   120
  182.       Width           =   1452
  183.    End
  184.    Begin VB.Line Line1 
  185.       X1              =   240
  186.       X2              =   9000
  187.       Y1              =   840
  188.       Y2              =   840
  189.    End
  190.    Begin VB.Label Label1 
  191.       Caption         =   "(No CERN) TIS Compatible Proxy:"
  192.       Height          =   252
  193.       Left            =   120
  194.       TabIndex        =   2
  195.       Top             =   1800
  196.       Width           =   2532
  197.    End
  198. Attribute VB_Name = "frmVBFTPJR"
  199. Attribute VB_GlobalNameSpace = False
  200. Attribute VB_Creatable = False
  201. Attribute VB_PredeclaredId = True
  202. Attribute VB_Exposed = False
  203. Dim bActiveSession As Boolean
  204. Dim hOpen As Long, hConnection As Long
  205. Dim dwType As Long
  206. Dim EnumItemNameBag As New Collection
  207. Dim EnumItemAttributeBag As New Collection
  208. Private Sub Form_Load()
  209.     bActiveSession = False
  210.     hOpen = 0
  211.     hConnection = 0
  212.     chkPassive.Value = 1
  213.     optBin.Value = 1
  214.     dwType = FTP_TRANSFER_TYPE_BINARY
  215.     Dim imgI As ListImage
  216.     Set imgI = ImageList1.ListImages.Add(, "open", LoadPicture("open.bmp"))
  217.     Set imgI = ImageList1.ListImages.Add(, "closed", LoadPicture("closed.bmp"))
  218.     Set imgI = ImageList1.ListImages.Add(, "leaf", LoadPicture("leaf.bmp"))
  219.     Set imgI = ImageList1.ListImages.Add(, "root", LoadPicture("root.bmp"))
  220.     TreeView1.ImageList = ImageList1
  221.     TreeView1.Style = tvwTreelinesPictureText
  222.     EnableUI (False)
  223. End Sub
  224. Private Sub Form_Unload(Cancel As Integer)
  225.     cmdClosehOpen_Click
  226. End Sub
  227. Private Sub cmdInternetOpen_Click()
  228.     If Len(txtProxy.Text) <> 0 Then
  229.         hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PROXY, txtProxy.Text, vbNullString, 0)
  230.     Else
  231.         hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
  232.     End If
  233.     If hOpen = 0 Then ErrorOut Err.LastDllError, "InternetOpen"
  234.     EnableUI (True)
  235. End Sub
  236. Private Sub cmdClosehOpen_Click()
  237.     If hConnection <> 0 Then InternetCloseHandle (hConnection)
  238.     If hOpen <> 0 Then InternetCloseHandle (hOpen)
  239.     hConnection = 0
  240.     hOpen = 0
  241.     If bActiveSession Then TreeView1.Nodes.Remove txtServer.Text
  242.     bActiveSession = False
  243.     ClearTextBoxAndBag
  244.     EnableUI (False)
  245. End Sub
  246. Private Sub cmdConnect_Click()
  247.     If Not bActiveSession And hOpen <> 0 Then
  248.         If txtServer.Text = "" Then
  249.             MsgBox "Please enter a server name!"
  250.             Exit Sub
  251.         End If
  252.         Dim nFlag As Long
  253.         If chkPassive.Value Then
  254.             nFlag = INTERNET_FLAG_PASSIVE
  255.         Else
  256.             nFlag = 0
  257.         End If
  258.         hConnection = InternetConnect(hOpen, txtServer.Text, INTERNET_INVALID_PORT_NUMBER, _
  259.         txtUser, txtPassword, INTERNET_SERVICE_FTP, nFlag, 0)
  260.         If hConnection = 0 Then
  261.             bActiveSession = False
  262.             ErrorOut Err.LastDllError, "InternetConnect"
  263.         Else
  264.             bActiveSession = True
  265.             EnableUI (CBool(hOpen))
  266.             FillTreeViewControl (txtServer.Text)
  267.             FtpEnumDirectory ("")
  268.             If EnumItemNameBag.Count = 0 Then Exit Sub
  269.             FillTreeViewControl (txtServer.Text)
  270.        End If
  271.     End If
  272. End Sub
  273. Private Sub cmdDisconnect_Click()
  274.     bDirEmpty = True
  275.     If hConnection <> 0 Then InternetCloseHandle hConnection
  276.     hConnection = 0
  277.     ClearBag
  278.     TreeView1.Nodes.Remove txtServer.Text
  279.     bActiveSession = False
  280.     EnableUI (True)
  281. End Sub
  282. Private Sub ClearTextBoxAndBag()
  283.     txtServer.Text = ""
  284.     txtUser.Text = ""
  285.     txtPassword.Text = ""
  286.     txtProxy.Text = ""
  287.     ClearBag
  288. End Sub
  289. Private Sub ClearBag()
  290.     Dim Num As Integer
  291.     For Num = 1 To EnumItemNameBag.Count
  292.         EnumItemNameBag.Remove 1
  293.     Next Num
  294.     For Num = 1 To EnumItemAttributeBag.Count
  295.         EnumItemAttributeBag.Remove 1
  296.     Next Num
  297. End Sub
  298. Private Sub FillTreeViewControl(strParentKey As String)
  299.     Dim nodX As Node
  300.     Dim strImg As String
  301.     Dim nCount As Integer, i As Integer
  302.     Dim nAttr As Integer
  303.     Dim strItem As String
  304.     If EnumItemNameBag.Count = 0 And strParentKey = txtServer.Text Then
  305.         Set nodX = TreeView1.Nodes.Add(, tvwFirst, txtServer.Text, txtServer.Text, "root")
  306.         Exit Sub
  307.     End If
  308.     nCount = EnumItemAttributeBag.Count
  309.     If nCount = 0 Then Exit Sub
  310.     For i = 1 To nCount
  311.         nAttr = EnumItemAttributeBag.Item(i)
  312.         strItem = EnumItemNameBag(i)
  313.         If nAttr = FILE_ATTRIBUTE_DIRECTORY Then
  314.             strImg = "closed"
  315.         Else
  316.             strImg = "leaf"
  317.         End If
  318.         Set nodX = TreeView1.Nodes.Add(strParentKey, tvwChild, strParentKey & "/" & strItem, _
  319.             strParentKey & "/" & strItem, strImg)
  320.     Next
  321.     nodX.EnsureVisible
  322. End Sub
  323. Private Sub cmdGet_Click()
  324.     Dim bRet As Boolean
  325.     Dim szFileRemote As String, szDirRemote As String, szFileLocal As String
  326.     Dim szTempString As String
  327.     Dim nPos As Long, nTemp As Long
  328.     Dim nodX As Node
  329.     Set nodX = TreeView1.SelectedItem
  330.     If bActiveSession Then
  331.         If nodX Is Nothing Then
  332.             MsgBox "Please select the item to GET!"
  333.             Exit Sub
  334.         End If
  335.         szTempString = TreeView1.SelectedItem.Text
  336.         szFileRemote = szTempString
  337.         nPos = 0
  338.         nTemp = 0
  339.         Do
  340.             nTemp = InStr(1, szTempString, "/", vbBinaryCompare)
  341.             If nTemp = 0 Then Exit Do
  342.             szTempString = Right(szTempString, Len(szTempString) - nTemp)
  343.             nPos = nTemp + nPos
  344.         Loop
  345.         szDirRemote = Left(szFileRemote, nPos)
  346.         szFileRemote = Right(szFileRemote, Len(szFileRemote) - nPos)
  347.         szFileLocal = File1.Path
  348.         rcd szDirRemote
  349.         bRet = FtpGetFile(hConnection, szFileRemote, szFileLocal & "/" & szFileRemote, False, _
  350.         INTERNET_FLAG_RELOAD, dwType, 0)
  351.         File1.Refresh
  352.         If bRet = False Then ErrorOut Err.LastDllError, "FtpGetFile"
  353.     Else
  354.         MsgBox "Not in session"
  355.     End If
  356. End Sub
  357. Private Sub cmdPut_Click()
  358.     Dim bRet As Boolean
  359.     Dim szFileRemote As String, szDirRemote As String, szFileLocal As String
  360.     Dim szTempString As String
  361.     Dim nPos As Long, nTemp As Long
  362.     Dim nodX As Node
  363.     Set nodX = TreeView1.SelectedItem
  364.     If bActiveSession Then
  365.         If nodX Is Nothing Then
  366.             MsgBox "Please select a remote directory to PUT to!"
  367.             Exit Sub
  368.         End If
  369.         If nodX.Image = "leaf" Then
  370.             MsgBox "Please select a remote directory to PUT to!"
  371.             Exit Sub
  372.         End If
  373.         If File1.filename = "" Then
  374.             MsgBox "Please select a local file to put"
  375.             Exit Sub
  376.         End If
  377.         szTempString = nodX.Text
  378.         szDirRemote = Right(szTempString, Len(szTempString) - Len(txtServer.Text))
  379.         szFileRemote = File1.filename
  380.         szFileLocal = File1.Path & "\" & File1.filename
  381.         If (szDirRemote = "") Then szDirRemote = "\"
  382.         rcd szDirRemote
  383.         
  384.         bRet = FtpPutFile(hConnection, szFileLocal, szFileRemote, _
  385.          dwType, 0)
  386.         If bRet = False Then
  387.             ErrorOut Err.LastDllError, "FtpPutFile"
  388.             Exit Sub
  389.         End If
  390.         
  391.         Dim nodChild As Node, nodNextChild As Node
  392.         Set nodChild = nodX.Child
  393.         Do
  394.           If nodChild Is Nothing Then Exit Do
  395.           Set nodNextChild = nodChild.Next
  396.             TreeView1.Nodes.Remove nodChild.Index
  397.             If nodNextChild Is Nothing Then Exit Do
  398.             Set nodChild = nodNextChild
  399.         Loop
  400.         If nodX.Image = "closed" Then
  401.             nodX.Image = "open"
  402.         End If
  403.         FtpEnumDirectory (nodX.Text)
  404.         FillTreeViewControl (nodX.Text)
  405.    End If
  406. End Sub
  407. Private Sub Dir1_Change()
  408.     File1.Path = Dir1.Path
  409. End Sub
  410. Private Sub Drive1_Change()
  411.     On Error GoTo ErrProc
  412.     Dir1.Path = Drive1.Drive
  413.     Exit Sub
  414. ErrProc:
  415.     Drive1.Drive = "c:"
  416.     Dir1.Path = Drive1.Drive
  417. End Sub
  418. Private Sub rcd(pszDir As String)
  419.     If pszDir = "" Then
  420.         MsgBox "Please enter the directory to CD"
  421.         Exit Sub
  422.     Else
  423.         Dim sPathFromRoot As String
  424.         Dim bRet As Boolean
  425.         If InStr(1, pszDir, txtServer.Text) Then
  426.         sPathFromRoot = Mid(pszDir, Len(txtServer.Text) + 1, Len(pszDir) - Len(txtServer.Text))
  427.         Else
  428.         sPathFromRoot = pszDir
  429.         End If
  430.         If sPathFromRoot = "" Then sPathFromRoot = "/"
  431.         bRet = FtpSetCurrentDirectory(hConnection, sPathFromRoot)
  432.         If bRet = False Then ErrorOut Err.LastDllError, "rcd"
  433.     End If
  434. End Sub
  435. Function ErrorOut(dError As Long, szCallFunction As String)
  436.     Dim dwIntError As Long, dwLength As Long
  437.     Dim strBuffer As String
  438.     If dError = ERROR_INTERNET_EXTENDED_ERROR Then
  439.         InternetGetLastResponseInfo dwIntError, vbNullString, dwLength
  440.         strBuffer = String(dwLength + 1, 0)
  441.         InternetGetLastResponseInfo dwIntError, strBuffer, dwLength
  442.         
  443.         MsgBox szCallFunction & " Extd Err: " & dwIntError & " " & strBuffer
  444.        
  445.         
  446.     End If
  447.     If MsgBox(szCallFunction & " Err: " & dError & _
  448.         vbCrLf & "Close Connection and Session?", vbYesNo) = vbYes Then
  449.         If hConnection Then InternetCloseHandle hConnection
  450.         If hOpen Then InternetCloseHandle hOpen
  451.         hConnection = 0
  452.         hOpen = 0
  453.         If bActiveSession Then TreeView1.Nodes.Remove txtServer.Text
  454.         bActiveSession = False
  455.         ClearTextBoxAndBag
  456.         EnableUI (False)
  457.     End If
  458. End Function
  459. Private Sub EnableUI(bEnabled As Boolean)
  460.     txtServer.Enabled = bEnabled
  461.     txtUser.Enabled = bEnabled
  462.     txtPassword.Enabled = bEnabled
  463.     cmdConnect.Enabled = bEnabled And Not bActiveSession
  464.     cmdDisconnect.Enabled = bEnabled And bActiveSession
  465.     chkPassive.Enabled = bEnabled
  466.     cmdClosehOpen.Enabled = bEnabled
  467.     cmdInternetOpen.Enabled = Not bEnabled
  468.     txtProxy.Enabled = Not bEnabled
  469.     optBin.Enabled = bEnabled
  470.     optAscii.Enabled = bEnabled
  471.     cmdGet.Enabled = bEnabled And bActiveSession
  472.     cmdPut.Enabled = bEnabled And bActiveSession
  473. End Sub
  474. Private Sub FtpEnumDirectory(strDirectory As String)
  475.     ClearBag
  476.     Dim hFind As Long
  477.     Dim nLastError As Long
  478.     Dim dError As Long
  479.     Dim ptr As Long
  480.     Dim pData As WIN32_FIND_DATA
  481.     If Len(strDirectory) > 0 Then rcd (strDirectory)
  482.     pData.cFileName = String(MAX_PATH, 0)
  483.     hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
  484.     nLastError = Err.LastDllError
  485.     If hFind = 0 Then
  486.         If (nLastError = ERROR_NO_MORE_FILES) Then
  487.             MsgBox "This directory is empty!"
  488.         Else
  489.             ErrorOut nLastError, "FtpFindFirstFile"
  490.         End If
  491.         Exit Sub
  492.     End If
  493.     dError = NO_ERROR
  494.     Dim bRet As Boolean
  495.     Dim strItemName As String
  496.     EnumItemAttributeBag.Add pData.dwFileAttributes
  497.     strItemName = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
  498.     EnumItemNameBag.Add strItemName
  499.     Do
  500.         pData.cFileName = String(MAX_PATH, 0)
  501.         bRet = InternetFindNextFile(hFind, pData)
  502.         If Not bRet Then
  503.             dError = Err.LastDllError
  504.             If dError = ERROR_NO_MORE_FILES Then
  505.                 Exit Do
  506.             Else
  507.                 ErrorOut dError, "InternetFindNextFile"
  508.                 InternetCloseHandle (hFind)
  509.                Exit Sub
  510.             End If
  511.         Else
  512.             EnumItemAttributeBag.Add pData.dwFileAttributes
  513.             strItemName = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
  514.             EnumItemNameBag.Add strItemName
  515.        End If
  516.     Loop
  517.     InternetCloseHandle (hFind)
  518. End Sub
  519. Private Sub optAscii_Click()
  520.     dwType = FTP_TRANSFER_TYPE_ASCII
  521. End Sub
  522. Private Sub optBin_Click()
  523.     dwType = FTP_TRANSFER_TYPE_BINARY
  524. End Sub
  525. Private Sub TreeView1_DblClick()
  526.     Dim nodX As Node
  527.     Set nodX = TreeView1.SelectedItem
  528.     If Not bActiveSession Then
  529.         MsgBox "No in session!"
  530.         Exit Sub
  531.     End If
  532.     If nodX Is Nothing Then
  533.         MsgBox "no Selection to enumerate"
  534.     End If
  535.     If nodX.Image = "closed" Then
  536.         nodX.Image = "open"
  537.         FtpEnumDirectory (nodX.Text)
  538.         FillTreeViewControl (nodX.Text)
  539.     Else
  540.         If nodX.Image = "open" Then
  541.             nodX.Image = "closed"
  542.             Dim nodChild As Node, nodNextChild As Node
  543.             Set nodChild = nodX.Child
  544.             Do
  545.             Set nodNextChild = nodChild.Next
  546.                 TreeView1.Nodes.Remove nodChild.Index
  547.                 If nodNextChild Is Nothing Then Exit Do
  548.                 Set nodChild = nodNextChild
  549.             Loop
  550.         End If
  551.     End If
  552. End Sub
  553.